#include "stdafx.h"
#include "LuaVM.h"
#include <sstream>
#include <luabind/luabind.hpp>

CLuaVM::CLuaVM( lua_State * L )
{
    m_pLuaState = lua_newthread(L);
}

CLuaVM::~CLuaVM( void )
{
    if (m_pLuaState)
        lua_close(m_pLuaState);
}

bool CLuaVM::LoadFile( const char * filename )
{
    std::ostringstream command;
    command << "package.loaded[\"" << filename << "\"]=nil;" 
        << "require \"" << filename << '\"';
    int error = luaL_loadbuffer(m_pLuaState, command.str().c_str(), command.str().length(),
        "line") || lua_pcall(m_pLuaState, 0, 0, 0);
    if (error) {
        m_strLastError = lua_tostring(m_pLuaState, -1);
        lua_pop(m_pLuaState, 1);/* pop error message from the stack */
        return false;
    }
    return true;
}

void CLuaVM::AddPath( const char * path )
{
    luabind::call_function<void>(m_pLuaState, "addpath", path);
}


